//删除链表中等于给定值 val 的所有节点。 
//
// 示例: 
//
// 输入: 1->2->6->3->4->5->6, val = 6
//输出: 1->2->3->4->5
// 
// Related Topics 链表 
// 👍 418 👎 0

/**
 * Question Number: 203
 * Current    Date: 2020-08-04 02:35:28
 */
package com.asimes.leetcode.editor.cn;

public class _203_RemoveLinkedListElements {
    public static void main(String[] args) {
        Solution solution = new _203_RemoveLinkedListElements().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode removeElements(ListNode head, int val) {
            while (head != null && head.val == val) {
                head = head.next;
            }
            if (head == null) {
                return null;
            }
            ListNode tmpHead = head;
            while (tmpHead.next != null) {
                if (tmpHead.next.val == val) {
                    tmpHead.next = tmpHead.next.next;
                } else {
                    tmpHead = tmpHead.next;
                }
            }

            return head;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}